package com.lizardmind.haofudoctor.ui.studio.gongGao

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityGongGaoAndSetBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_gong_gao_and_set.*
import kotlinx.android.synthetic.main.include_title_right_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.textColor
import org.jetbrains.anko.toast

class GongGaoAndSetActivity : BaseActivity() {

    val binding by lazy { getDataBinding<ActivityGongGaoAndSetBinding>(R.layout.activity_gong_gao_and_set) }
    override fun onCreatRootView(): View? {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text="公告及主页设置"
        tvRight.text="预览"
        tvRight.textColor=ContextCompat.getColor(this,R.color.notice_red)
        tvRight.onClick { toast("预览") }

        getDoctorData()

        tvGongGao.onClick { intentActivity("gonggao") }
        tvIntroduce.onClick { intentActivity("introduction") }
        tvGoodAt.onClick { intentActivity("good_at") }
        tvMenZhenDataSet.onClick { startActivity(Intent(this@GongGaoAndSetActivity,MenZhenDataSetActivity::class.java)) }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        binding.bjGonggao=DoctorInfo.gonggao
        binding.bjIntroduce=DoctorInfo.introduce
        binding.bjGoodAt=DoctorInfo.good_at
    }

    private fun intentActivity(tag:String){
        startActivityForResult(Intent(this@GongGaoAndSetActivity,GongGaoInputActivity::class.java).putExtra("tag",tag),1)
    }

    private fun getDoctorData(){
        observe {
            postApi<Request<DoctorBean>>(
                    hashMapOf("action" to "get_doctor_info",
                            "token" to DoctorInfo.token),
                    Consumer {
                        it.dispose {
                            binding.bean=it
                            binding.bjGonggao=it.gonggao
                            binding.bjIntroduce=it.introduction
                            binding.bjGoodAt=it.good_at
                            DoctorInfo.introduce=it.introduction
                            DoctorInfo.gonggao=it.gonggao
                            DoctorInfo.good_at=it.good_at
                        }
                    },context = this@GongGaoAndSetActivity)
        }

    }

}
